package com.asimplefox.service.impl;

import com.asimplefox.configs.FileConfig;
import com.asimplefox.entity.Carousel;
import com.asimplefox.entity.vo.CarouselVo;
import com.asimplefox.mapper.CarouselMapper;
import com.asimplefox.service.ICarouselService;
import com.asimplefox.service.abst.ServiceImplAll;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import io.github.kloping.judge.Judge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kloping
 * @since 2024-09-30
 */
@Service
public class CarouselServiceImpl extends ServiceImplAll<CarouselMapper, Carousel> implements ICarouselService {

    @Autowired
    FileConfig fileConfig;

    @Override
    public String saveCarousel(CarouselVo carouselVo) {
        if (carouselVo == null) {
            return result(-1, "图片地址不能为空");
        } else if (Judge.isEmpty(carouselVo.getJump())) {
            return result(-1, "跳转链接不能为空");
        } else {
            Carousel c0 = lambdaQuery().eq(Carousel::getSt, carouselVo.getSt()).one();
            if (c0 != null) {
                return result(-1, "该排序号已存在");
            } else {
                Carousel carousel = packTo(carouselVo);
                if (carousel == null) return result(-2, "封面上传失败");
                if (save(carousel)) return result(1, "上传成功", "id", carousel.getId());
                else return result(-1, "上传失败");
            }
        }
    }

    @Override
    public String updateOne(Integer id, CarouselVo carouselVo) {
        if (carouselVo == null) {
            return result(-1, "图片地址不能为空");
        } else if (Judge.isEmpty(carouselVo.getJump())) {
            return result(-1, "跳转链接不能为空");
        } else {
            Carousel c0 = lambdaQuery().eq(Carousel::getSt, carouselVo.getSt()).one();
            if (c0 != null && c0.getId() != id) {
                return result(-1, "该排序号已存在");
            } else {
                c0 = lambdaQuery().eq(Carousel::getId, id).one();
                carouselVo.setSt(c0.getSt());
                Carousel carousel = packTo(carouselVo);
                if (carousel == null) return result(-2, "封面上传失败");
                if (lambdaUpdate().eq(Carousel::getId, id).update(carousel))
                    return result(1, "更新成功");
                else return result(-1, "更新失败");
            }
        }
    }

    private Carousel packTo(CarouselVo carouselVo) {
        Carousel carousel = new Carousel();
        carousel.setSt(getNextSt(carouselVo));
        if (carouselVo.getSt() == null) carouselVo.setSt(carousel.getSt());
        FileConfig.FileWithPath fileWithPath = fileConfig.getFileWithPath("carousel/", carouselVo.getSt() + ".jpg");
        try {
            fileWithPath.write(carouselVo.getUrl().getBytes());
        } catch (IOException e) {
            return null;
        }
        carousel.setUrl(fileWithPath.getUrl());
        carousel.setSt(carouselVo.getSt());
        carousel.setJump(carouselVo.getJump());
        carousel.setTitle(carouselVo.getTitle());
        carousel.setDesc(carouselVo.getDesc());
        carousel.setTime(LocalDate.now());
        return carousel;
    }

    private Integer getNextSt(CarouselVo vo) {
        if (vo.getSt() != null) return vo.getSt();
        LambdaQueryChainWrapper<Carousel> qw = lambdaQuery();
        qw.select(Carousel::getSt).orderByDesc(Carousel::getSt).last("limit 0,1");
        Carousel st1 = qw.one();
        if (st1 == null) return 1;
        return st1.getSt() + 1;
    }
}
